/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.staff;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.staff.AplicacaoInterface;
import br.gov.cemaden.library.model.staff.Aplicacao;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class AplicacaoDao extends GenericDao implements AplicacaoInterface {

    private Session session;

    public AplicacaoDao(Session session) {
        this.session = session;
    }

    public AplicacaoDao() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Aplicacao> findAll() {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Aplicacao> aplicacoes = null;
        try {
            Query query = session.getNamedQuery("Aplicacao.findAll");
            aplicacoes = (List<Aplicacao>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no Aplicacao.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return aplicacoes;
    }

    @Override
    public Aplicacao findById(Long id) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Aplicacao aplicacao = null;
        try {
            Query query = session.getNamedQuery("Aplicacao.findById").setParameter("id", id);
            aplicacao = (Aplicacao) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no Aplicacao.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return aplicacao;
    }

    @Override
    public Aplicacao findByNome(String nome) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Aplicacao aplicacao = null;
        try {
            Query query = session.getNamedQuery("Aplicacao.findByNome").setParameter("nome", nome);
            aplicacao = (Aplicacao) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no Aplicacao.findByNome: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return aplicacao;
    }

    @Override
    public List<Aplicacao> findByTempo(Integer tempo) {
        //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Aplicacao> aplicacoes = null;
        try {
            Query query = session.getNamedQuery("Aplicacao.findByTempo").setParameter("tempo", tempo);
            aplicacoes = (List<Aplicacao>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no Aplicacao.findByTempo: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return aplicacoes;
    }    

    /**
     * Este método "insert" poderia ser 'implements' de uma Interface e usar por
     * herança, 'extends', o saveOrUpdate(app); da classe AbastractFacade, pois
     * extends permite o uso direto do método
     *
     * @param app
     */
    @Override
    public void insert(Aplicacao app) {
        try {
            saveOrUpdate(this.session, app);
        } catch (Exception ex) {
            Logger.getLogger(AplicacaoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Aplicacao app) {
        try {
            saveOrUpdate(this.session, app);
        } catch (Exception ex) {
            Logger.getLogger(AplicacaoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Aplicacao app) {
        try {
            remove(this.session, app);

        } catch (Exception ex) {
            Logger.getLogger(AplicacaoDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
